package com.winit.common.openApi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * Created by wanglin on 2018/5/17.
 */
public class OpenapiLogUtil {
    private static List<String> actions=new ArrayList<String>();
    private static String openapiPath;
    static {
        try {
            Properties properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
            openapiPath = properties.getProperty("openapi.log.path");
        } catch (Exception e) {
        }

    }

    public static List<JSONObject> analysis() throws IOException {
        List<JSONObject> jsonArr = new ArrayList<JSONObject>();
        List<File> files = getFiles();
        for(File file:files){
            FileReader reader = new FileReader(file);
            //new一个BufferedReader对象，将文件内容读取到缓存
            BufferedReader bReader = new BufferedReader(reader);
            //定义一个字符串缓存，将字符串存放缓存中
            String s;
            //逐行读取文件内容，不读取换行符和末尾的空格
            while ((s = bReader.readLine()) != null) {
                if(s.contains("LogInterceptor")){
                    if(s.matches("([\\s\\S]*)(\\|\\/)(api|service)(\\|[0-9.\\|]*)([\\s\\S]*)")){
                        String resultStr = s.replaceAll("([\\s\\S]*)(\\|\\/)(api|service)(\\|[0-9.\\|]*)([\\s\\S]*)", "$5");
                        if(resultStr.matches("([\\s\\S]*)(\\})(\\|\\|)(\\{)([\\s\\S]*)")){
                            JSONObject result =  JSONObject.parseObject(resultStr.replaceAll("([\\s\\S]*)(\\})(\\|\\|)(\\{)([\\s\\S]*)", "$1$2"));
                            if(!actions.contains(result.get("action"))){
                                actions.add((String) result.get("action"));
                                JSONObject json = new JSONObject();
                                JSONObject rtn =  JSONObject.parseObject(resultStr.replaceAll("([\\s\\S]*)(\\})(\\|\\|)(\\{)([\\s\\S]*)", "$4$5"));
                                json.put("action",result.get("action"));
                                json.put("entity",JsonObjectUtil.parseJsonString(result));
                                json.put("rtn",JsonObjectUtil.parseJsonString(rtn));
                                jsonArr.add(json);
                            }
                        }
                    }
                }
            }
            bReader.close();
        }
        return jsonArr;
    }

    private static List<File> getFiles(){
        File file = new File(openapiPath);
        File[] fileList = file.listFiles();
        //新建一个文件集合
        List<File> wjList = new ArrayList<File>();
        for (int i = 0; i < fileList.length; i++) {
            //判断是否为文件
            if (fileList[i].isFile() && fileList[i].getName().contains(".log")) {
                wjList.add(fileList[i]);
            }
        }
        return wjList;
    }


    public static void main(String[] args) throws IOException {
                analysis();
    }
}
